package com.fileutils;

import com.jniutils.DataCollUtil;
import java.util.ArrayList;
import static com.fileutils.BaseConstant.*;

/**
 * Created by Administrator on 2019/5/7.
 */

public class NumberUtils {
    private static final String[] units = {"千", "百", "十", ""};//个位
    private static final String[] bigUnits ={"万","亿"};
    private static final char[] numChars ={'一', '二', '三', '四', '五', '六', '七', '八', '九'};
    private static final String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
    private static final String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };

    /**
     * 将String中的中文数字转化为阿拉伯数字
     *
     * @param sourceData
     * @return
     */
    public static String CharDateFormat(String sourceData) {
        ArrayList<String> mNumberArray = new ArrayList<>();
        String mNumberCache = "";
        String mCharCache = "";

        for (int index = 0; index <= sourceData.length() - 1; index++) {
            String mCharNum = sourceData.substring(index, index + 1);
            if (CHAR_NUMBER.contains(mCharNum)) {
                if (!mCharCache.equals("")) {
                    String str = mCharCache;
                    mNumberArray.add(str);
                    mCharCache = "";
                }
                mNumberCache = mNumberCache + mCharNum;
            } else {
                if (!mNumberCache.equals("")) {
                    String str = mNumberCache;
                    mNumberArray.add(str);
                    mNumberCache = "";
                }
                if (mCharNum.equals("点")) {
                    mCharNum = ".";
                }
                mCharCache = mCharCache + mCharNum;
            }
        }

        if (!mNumberCache.equals("")) {
            mNumberArray.add(mNumberCache);
        }

        if (!mCharCache.equals("")) {
            mNumberArray.add(mCharCache);
        }
        return brandNewDateFormat(mNumberArray);
    }

    /**
     * 中文数字转阿拉伯数字部分需要特殊处理
     *
     * @param arr
     * @return
     */
    private static String brandNewDateFormat(ArrayList<String> arr) {
        String mResultValue = "";
        for (int count = 0; count < arr.size(); count++) {
            String result=DataCollUtil.patternValue(PATTERN_CHAR,arr.get(count));
            if (result!=null&&!result.equals("")){
                String timeValue = "";
                int numValue = com.fileutils.NumberUtils.numberCN2Arab(arr.get(count));
                timeValue = "" + numValue;
                if (count >= 1) {
                    if (arr.get(count - 1).substring(arr.get(count - 1).length() - 1).equals(".") && arr.get(count).substring(0, 1).equals("零")) {
                        timeValue = "0" + numValue;
                    }
                }
                arr.set(count, timeValue);
            }
            mResultValue = mResultValue + arr.get(count);
        }
        return mResultValue;
    }

    /**
     * 阿拉伯数字转中文数字   阿拉伯数字格式：   18:20
     *
     * @return
     */
    public static String ArabDateFormat(String sourceData) {
        String hourStr = "";
        String minuteStr = "";
        String[] mTimeArray = sourceData.split(":");
        if (mTimeArray != null && mTimeArray.length != 0) {
            if (mTimeArray[0].substring(0, 1).equals("0")) {          //时
                hourStr =numberCharArab2CN(mTimeArray[0].substring(1));
            } else {
                hourStr =numberCharArab2CN(mTimeArray[0]);
            }

            if (mTimeArray.length >= 2 && mTimeArray[0] != null) {
                minuteStr =numberCharArab2CN(mTimeArray[1]);
            }
        }
        if (!minuteStr.equals("零零")) {
            return CharDateTime(hourStr) + "时" + CharDateMinute(minuteStr) + "分";
        } else {
            return CharDateTime(hourStr) + "时整";
        }
    }

    /**
     * 时间中分钟部分单独处理
     *
     * @return
     */
    private static String CharDateMinute(String minute) {
        String minuteTimeStr = CharDateTime(minute);
        switch (minuteTimeStr) {
            case "三十零":
                return "三十";
            case "四十零":
                return "四十";
            case "五十零":
                return "五十";
        }
        return minuteTimeStr;
    }

    /**
     * 时间中小时部分单独处理
     *
     * @return
     */
    private static String CharDateTime(String mTime) {
        switch (mTime) {
            case "一十零":
                return "十";
            case "一十一":
                return "十一";
            case "一十二":
                return "十二";
            case "一十三":
                return "十三";
            case "一十四":
                return "十四";
            case "一十五":
                return "十五";
            case "一十六":
                return "十六";
            case "一十七":
                return "十七";
            case "一十八":
                return "十八";
            case "一十九":
                return "十九";
            case "二十零":
                return "二十";
        }
        return mTime;
    }

    /**
     * 将阿拉伯数字转换为中文数字
     * @param numberStr
     * @return
     */
    public static String numberCharArab2CN(String numberStr){
        String result = "";
        int n = numberStr.length();
        for (int i = 0; i < n; i++) {
            int num = numberStr.charAt(i) - '0';
            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
        }
        return result;
    }

    /**
     * 将中文数字转换为阿拉伯数字;
     *
     * @return
     * @paramnumberCN
     */
    public static int numberCN2Arab(String numberCN){
        String tempNumberCN = numberCN;
        if (tempNumberCN == null) {
            return 0;
        }
        String[] nums = new String[bigUnits.length + 1];
        nums[0] = tempNumberCN;
        for (int i = (bigUnits.length - 1); i >= 0; i--) {
            int find = tempNumberCN.indexOf(bigUnits[i]);
            if (find != -1) {
                String[] tempStrs = tempNumberCN.split(bigUnits[i]);
                if (nums[0] != null) {
                    nums[0] = null;
                }
                if (tempStrs.length>0&&tempStrs[0] != null) {
                    nums[i + 1] = tempStrs[0];
                }
                if (tempStrs.length > 1) {
                    tempNumberCN = tempStrs[1];
                    if (i == 0) {
                        nums[0] = tempStrs[1];
                    }
                } else {
                    tempNumberCN = null;
                    break;
                }
            }
        }
        String tempResultNum = "";
        for (int i = nums.length - 1; i >= 0; i--) {
            if (nums[i] != null) {
                tempResultNum += numberKCN2Arab(nums[i]);
            } else {
                tempResultNum += "0000";
            }
        }
        return Integer.parseInt(tempResultNum);
    }


    /**
     * 将一位中文数字转换为一位数字;eg:一返回1;
     *
     * @return
     * @paramonlyCNNumber
     */
    public static int numberCharCN2Arab(char onlyCNNumber){
        if (numChars[0] == onlyCNNumber) {
            return 1;
        } else if (numChars[1] == onlyCNNumber || onlyCNNumber == '两') {//处理中文习惯用法(二,两)
            return 2;
        } else if (numChars[2] == onlyCNNumber) {
            return 3;
        } else if (numChars[3] == onlyCNNumber) {
            return 4;
        } else if (numChars[4] == onlyCNNumber) {
            return 5;
        } else if (numChars[5] == onlyCNNumber) {
            return 6;
        } else if (numChars[6] == onlyCNNumber) {
            return 7;
        } else if (numChars[7] == onlyCNNumber) {
            return 8;
        } else if (numChars[8] == onlyCNNumber) {
            return 9;
        }
        return 0;
    }

    /**
     * 处理千以内中文数字,返回4位数字字符串,位数不够以"0"补齐;
     *
     * @return
     * @paramnumberCN
     */
    private static String numberKCN2Arab(String numberCN) {
        if ("".equals(numberCN)) {
            return "";
        }
        int[] nums = new int[4];
        if (numberCN != null) {
            for (int i = 0; i < units.length; i++) {
                int idx = numberCN.indexOf(units[i]);
                if (idx > 0) {
                    char tempNumChar = numberCN.charAt(idx - 1);
                    int tempNumInt = numberCharCN2Arab(tempNumChar);
                    nums[i] = tempNumInt;
                }
            }
            char ones = numberCN.charAt(numberCN.length() - 1);
            nums[nums.length - 1] = numberCharCN2Arab(ones);
            if ((numberCN.length() == 2 || numberCN.length() == 1)
                    && numberCN.charAt(0) == '十') {
                nums[nums.length - 2] = 1;
            }
        }
        String tempNum = "";
        for (int i = 0; i < nums.length; i++) {
            tempNum += nums[i];
        }
        return (tempNum);
    }
}